package com.mock.ems.controller.config;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.mock.base.util.DateUtil;
import com.mock.base.util.StringUtil;
import com.mock.ems.common.JsonResult;

import java.util.*;

/**
 * Created by MOCK on 2017/8/24.
 */
public class NoticeController extends Controller {

    public void index() {

        renderJsp("/page/config/notice/notice_list.jsp");
    }

    public void edit() {
        Long id = getParaToLong("id");
        if (null != id) {
            Record record = Db.findById("notice",id);
            if (null != record) {
                setAttr("record",record.getColumns());
            }
        }
        renderJsp("/page/config/notice/notice_edit.jsp");
    }

    @Before(Tx.class)
    public void save() {

        Long id = getParaToLong("id");
        Record record = null;
        if (null == id) {
            record = new Record();
            record.set("status",0)
                    .set("create_time",new Date());
        }else {
            record = Db.findById("notice",id);
        }
        if (null == record) {
            renderJson(new JsonResult().status("-1"));
            return;
        }

            record.set("title",getPara("title"))
                .set("image",getPara("image"))
                .set("content",getPara("content"))
                .set("update_time",new Date());

        if (null == id) {
            if (!Db.save("notice",record)) {
                renderJson(new JsonResult().status("-2"));
                return;
            }
        }else {
            if (!Db.update("notice",record)) {
                renderJson(new JsonResult().status("-3"));
                return;
            }
        }
        renderJson(new JsonResult().status("000"));
    }

    public void pagination() {

        int pageNumber = getParaToInt("pageNumber");
        int pageSize = getParaToInt("pageSize");

        String selecSQL = "select id,title,create_time,status";

        StringBuilder exceptSelectSQL = new StringBuilder(" from notice where 1 = 1 ");
        List<Object> params = new ArrayList<Object>();

        String title = getPara("title");
        if (StringUtil.isNotEmpty(title)) {
            exceptSelectSQL.append(" and title like ? ");
            params.add('%' + title + '%');
        }

        Date begin_time = DateUtil.parseToDate(getPara("begin_time"),DateUtil.FULL_PATTERN);
        Date end_time = DateUtil.parseToDate(getPara("end_time"),DateUtil.FULL_PATTERN);

        if (null != begin_time && end_time == null) {
            exceptSelectSQL.append(" and create_time >= ? ");
            params.add(begin_time);
        }

        if (null == begin_time && end_time != null) {
            exceptSelectSQL.append(" and create_time <= ? ");
            params.add(end_time);
        }

        if (null != begin_time && null != end_time) {
            exceptSelectSQL.append(" and create_time >= ? and create_time <= ? ");
            params.add(begin_time);
            params.add(end_time);
        }

        exceptSelectSQL.append(" order by update_time desc ");
        Page<Record> recordList = null;
        if (params.size() == 0) {
            recordList = Db.paginate(pageNumber, pageSize, selecSQL, exceptSelectSQL.toString());
        }else {
            recordList = Db.paginate(pageNumber, pageSize, selecSQL, exceptSelectSQL.toString(),params.toArray());
        }

        Map<String, Object> returnMap = new HashMap<String, Object>();
        returnMap.put("total", recordList.getTotalRow());
        returnMap.put("rows", recordList.getList());
        renderJson(returnMap);
    }


    @Before(Tx.class)
    public void publish() {

        Long id = getParaToLong("id");
        if (null == id) {
            renderJson(new JsonResult().status("-1"));
            return;
        }
        if (Db.update("update notice set status = 1,update_time = ?,publish_time = ? where id = ? ",
            new Date(),new Date(),id) <= 0) {
            renderJson(new JsonResult().status("-2"));
            return;
        }
        renderJson(new JsonResult().status("000"));
    }

    @Before(Tx.class)
    public void delete() {

        String noticeIdList = getPara("noticeIdList");
        JsonResult result = new JsonResult();
        if (StringUtil.isEmpty(noticeIdList)) {
            renderJson(result.status("001").show("删除失败"));
            return;
        }

        StringBuilder deleteSQL = new StringBuilder("delete from notice where id in (");
        Object [] notices = new Object[noticeIdList.split(",").length];
        int index = 0;
        for (String id : noticeIdList.split(",")) {
            deleteSQL.append("?,");
            notices[index]= id;
            index ++;
        }
        deleteSQL.deleteCharAt(deleteSQL.length() - 1).append(")");

        boolean deleteResult = Db.update(deleteSQL.toString(),notices) == notices.length;
        renderJson(deleteResult ? result.status("000").show("删除成功"):result.status("002").show("删除失败"));
    }
}
